﻿// 当插件安装时触发
chrome.runtime.onInstalled.addListener(function () {
    console.log("Extension installed");
});

var itemListData = {
    'itemList': null
};

requestMap['getItemList'] = function (args, sender, sendResponse) {
    var result = null;
    chrome.storage.local.get({"itemList": null}, function (data) {
        sendResponse(data['itemList']);
    });
    return true;
};

requestMap['setItem'] = function (args, sender, sendResponse) {
    chrome.storage.local.get({"itemList": null}, function (data) {
        var itemList;
        if (data == null || data['itemList'] == null) {
            itemList = [];
        } else {
            itemList = data['itemList'];
        }
        for (var i = 0; i < args.length; i++) {
            itemList[itemList.length] = args[i];
        }
        chrome.storage.local.set({'itemList': itemList}, function () {
            sendResponse(CommonResponse.ok());
        });

    });
    return true;
};

requestMap['removeById'] = function (args, sender, sendResponse) {
    chrome.storage.local.get({"itemList": null}, function (data) {
        itemList = data['itemList'];
        itemList = itemList.filter(function (item) {
            if (item['id'] == args['id']) {
                return false;
            }
            return true;
        });
        chrome.storage.local.set({'itemList': itemList}, function () {
            sendResponse("success");
        });

    });
    return true;
};

requestMap['update'] = function (args, sender, sendResponse) {
    chrome.storage.local.get({"itemList": null}, function (data) {
        itemList = data['itemList'];
        for (var i = 0; i < itemList.length; i++) {
            if (itemList[i]['id'] == args['id']) {
                itemList[i] = args;
            }
        }
        chrome.storage.local.set({'itemList': itemList}, function () {
            sendResponse(CommonResponse.ok());
        });

    });
    return true;
}

handler.onHeadersReceived[handler.onHeadersReceived.length] = {
    match: function (details) {
        return true;
    },
    execute: function (details) {
        var needAdd = false;
        for (var i = 0; i < details.responseHeaders.length; i++) {
            var cur = details.responseHeaders[i];
            if (cur.name == 'Access-Control-Allow-Origin') {
                needAdd = false;
                cur.value = "*";
                break;
            }
        }
        if (needAdd) {
            details.responseHeaders.push({name: 'Access-Control-Allow-Origin', value: "*"});
        }
        for (var i = 0; i < details.responseHeaders.length; i++) {
            var cur = details.responseHeaders[i];
            if (cur.name == 'Access-Control-Allow-Methods') {
                needAdd = false;
                cur.value = "*";
                break;
            }
        }
        if (needAdd) {
            details.responseHeaders.push({name: 'Access-Control-Allow-Methods', value: "*"});
        }
    }
};

handler.onBeforeRequest[handler.onBeforeRequest.length] = {
    match: function (details) {
        return true;
    },
    execute: function (details) {
        setTimeout(function () {
            chrome.storage.local.get({"itemList": null}, function (data) {
                itemListData['itemList'] = data['itemList'];
            });
        });

        var ssl = false;
        try {
            ssl = details.initiator.indexOf("https") >= 0;
        } catch (e) {
            ssl = details.url.indexOf("https") >= 0;
        }

        if (itemListData['itemList'] != null && typeof (itemListData['itemList']) != 'undefined') {
            var itemList = itemListData['itemList'];
            var url = details.url;
            for (var i = 0; i < itemList.length; i++) {
                if (itemList[i]['checked']) {
                    url = url.replace(itemList[i]['old'], itemList[i]['new']);
                }
            }
            if (url != details.url) {
                //console.log("background.js:"+details.url+"--->"+url);
                details.url = url;
            }
        }
    }
}

handler.onResponseStarted[handler.onResponseStarted.length] = {
    match: function (details) {
        return true;
    },
    execute: function (details) {
    }
}

function FindProxyForURL(url, host) {
    if(url.startsWith("http://dev.cs.beicaizs.com/api")){
        return 'PROXY test.cs.beicaizs.com';
    } else {
        return 'DIRECT';
    }
}



var config = {
    mode: "pac_script",
    pacScript: {
        data: FindProxyForURL.toString()
    }
};
chrome.proxy.settings.set(
    {value: config, scope: 'regular'},
    function () {
    }
);
